﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Collections;

namespace DiaryPlanner.Diary
{
    public partial class mod_diary : Form
    {
        Diary_main object_diary_main;
        private int align_value, emotion_value , weather_value;
        public string  all_date; //all_date - 저장된 file의 이름
        public int check_photoOrdraw; // 그림 또는 사진의 저장 유무를 파악
        

        public mod_diary(Diary_main diary_main)
        {
            object_diary_main = diary_main;
            InitializeComponent();
            align_value = 0;
            check_photoOrdraw = 0;
            init_mod_view();
         
        }
        private void init_mod_view()
        {
            /////////선택된 날짜정보를 받아온다
            select_year_text_box.Text = object_diary_main.load_year_value();
            select_month_text.Text = object_diary_main.load_month_value();
            select_day_text.Text = object_diary_main.load_day_value();
            all_date = select_year_text_box.Text + select_month_text.Text + select_day_text.Text;

            //////선택된 날짜의 text 정보를 받아온다.
            TextBox temp_text_box = new TextBox();
            temp_text_box = object_diary_main.load_dialy_text();
            mod_daily_diary_text.Text = temp_text_box.Text;
            mod_daily_diary_text.TextAlign = temp_text_box.TextAlign;
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            this.button4.Visible = true;
            this.button5.Visible = true;
            this.button6.Visible = true;
            this.button7.Visible = true;
            this.pictureBox4.Visible = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            emotion_value = 0;
            this.pictureBox5.Image = global::DiaryPlanner.Properties.Resources.smile_big;
            this.button4.Visible = false;
            this.button5.Visible = false;
            this.button6.Visible = false;
            this.button7.Visible = false;
            this.pictureBox4.Visible = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            emotion_value = 1;
            this.pictureBox5.Image = global::DiaryPlanner.Properties.Resources.sad_big;
            this.button4.Visible = false;
            this.button5.Visible = false;
            this.button6.Visible = false;
            this.button7.Visible = false;
            this.pictureBox4.Visible = false;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            emotion_value = 2;
            this.pictureBox5.Image = global::DiaryPlanner.Properties.Resources.soso_big;
            this.button4.Visible = false;
            this.button5.Visible = false;
            this.button6.Visible = false;
            this.button7.Visible = false;
            this.pictureBox4.Visible = false;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            emotion_value = 3;
            this.pictureBox5.Image = global::DiaryPlanner.Properties.Resources.angry_big;
            this.button4.Visible = false;
            this.button5.Visible = false;
            this.button6.Visible = false;
            this.button7.Visible = false;
            this.pictureBox4.Visible = false;
        }

        private void picture_photo_popup_button_Click(object sender, EventArgs e)
        {
            Diary.pic_draw_add picture_draw_add = new pic_draw_add(this);
            picture_draw_add.ShowDialog();


        }

        private void left_pic_box_Click(object sender, EventArgs e)
        {
            this.mod_daily_diary_text.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
            align_value = 0;

        }

        private void center_pic_box_Click(object sender, EventArgs e)
        {
            this.mod_daily_diary_text.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            align_value = 1;
        }

        private void right_pic_box_Click(object sender, EventArgs e)
        {
            this.mod_daily_diary_text.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            align_value = 2;
        }

        private void save_button_Click(object sender, EventArgs e)
        {
            object_diary_main.write_text_value(this.mod_daily_diary_text);
            object_diary_main.update_emotion(emotion_value, weather_value);
            update_db(this.mod_daily_diary_text.Text, (select_year_text_box.Text + select_month_text.Text + select_day_text.Text));
            
            object_diary_main.update_view(all_date);
            this.Dispose();
            this.Close();
        }

        private void update_db(string daily_diary_text, string date_text)
        {
            try
            {
                string connectionString = "provider=Microsoft.JET.OLEDB.4.0;" //연결하는 객체의 공급자명(확장자명)
                                         + "data source = "  //데이터 소스=(경로입력하라는거)
                                         + Application.StartupPath ///상대경로 읽어옴
                                         + @"\diaryplannerdb.mdb";//실제 데이터베이스 파일이름

                string commandString = "Select * from [diary]";//memos테이블에 있는 모든필드들을 모두 검색해라.

                OleDbDataAdapter DBAdapter ////DataSet을 채우고 데이터 소스를 업데이트하는 데 사용되는 데이터 명령 집합 및 데이터베이스 연결하게 하는 함수
                    = new OleDbDataAdapter(commandString, connectionString);//connectionString이라는 경로와 쿼리명령을 동적할당

                OleDbCommandBuilder myCommandBuilder ///연결된 데이터베이스에서 DataSet의 변경 내용을 조정하는 데 사용되는 단일 테이블 명령을 자동으로 생성합니다
                    = new OleDbCommandBuilder(DBAdapter);

                DataSet DS = new DataSet(); //DS라는 데이터 셋 동적할당

                DBAdapter.Fill(DS, "diary"); //DS라는 데이터셋에 새로운 memos 테이블에 추가 또는 갱신하겠다고 알림.

                DataTable aTable = DS.Tables["diary"];//memos테이블을 불러와 aTable을 새로 생성해서 저장

                DataColumn[] PrimaryKey = new DataColumn[1];

                PrimaryKey[0] = aTable.Columns["Ddate"]; //a컬럼을 기본키를 설정

                aTable.PrimaryKey = PrimaryKey;//기본키 할당



                DataRow currRow = aTable.Rows.Find(date_text + UserProfile.Instance().userId); //textBox값인 기본키 선택
                
                currRow.BeginEdit(); //수정을 할 부분 시작 설정
                currRow["daily_diary_text"] = daily_diary_text;
                currRow["align"] = align_value;
                currRow["emotion"] = emotion_value;

                if (check_photoOrdraw == 1)
                {
                    currRow["filename"] = all_date;
                    object_diary_main.check_photo_draw = 1;
                } 
                //currRow["id"] = UserProfile.Instance().userId;
                
                
                currRow.EndEdit(); //수정할 부분 끝 부분 설정

                DataSet UpdateSet = DS.GetChanges(DataRowState.Modified); //데이터 상태를 수정한다.
                if (UpdateSet.HasErrors)
                {
                    MessageBox.Show("변경된 데이터에 문제가 있습니다.");
                }
                else
                {
                    DBAdapter.Update(DS, "diary");
                    DS.AcceptChanges(); //실제 데이터를 생성
                }
            }
            catch (DataException DE)
            {
                MessageBox.Show(DE.Message);
            }
            catch (Exception DE)
            {
                MessageBox.Show(DE.Message);
            }
        }

        private void weather_view_Click(object sender, EventArgs e)
        {
            this.sunny.Visible = true;
            this.cloud.Visible = true;
            this.rain.Visible = true;
            this.snow.Visible = true;
            this.pictureBox1.Visible = true;
        }

        private void sunny_Click(object sender, EventArgs e)
        {
            weather_value = 0;
            this.weather_view.Image = global::DiaryPlanner.Properties.Resources.serenity_big;
            this.sunny.Visible = false;
            this.cloud.Visible = false;
            this.rain.Visible = false;
            this.snow.Visible = false;
            this.pictureBox1.Visible = false;
        }

        private void cloud_Click(object sender, EventArgs e)
        {
            weather_value = 1;
            this.weather_view.Image = global::DiaryPlanner.Properties.Resources.cloud_big;
            this.sunny.Visible = false;
            this.cloud.Visible = false;
            this.rain.Visible = false;
            this.snow.Visible = false;
            this.pictureBox1.Visible = false;
        }

        private void rain_Click(object sender, EventArgs e)
        {
            weather_value = 2;
            this.weather_view.Image = global::DiaryPlanner.Properties.Resources.rain_big;
            this.sunny.Visible = false;
            this.cloud.Visible = false;
            this.rain.Visible = false;
            this.snow.Visible = false;
            this.pictureBox1.Visible = false;
        }

        private void snow_Click(object sender, EventArgs e)
        {
            weather_value = 3;
            this.weather_view.Image = global::DiaryPlanner.Properties.Resources.snow_big;
            this.sunny.Visible = false;
            this.cloud.Visible = false;
            this.rain.Visible = false;
            this.snow.Visible = false;
            this.pictureBox1.Visible = false;
        }

        
    }
}
